/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.opgea.token.dao.DestinationDAO;
import com.opgea.token.domain.entities.Destination;

/**
 * 
 * @author Ramesh
 */
@Repository
public class DestinationDAOImpl implements DestinationDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Destination create(Destination destination) {
	sessionFactory.getCurrentSession().save(destination);
	return destination;
    }

    @Override
    public Destination update(Destination destination) {
	Destination dest = (Destination) sessionFactory.getCurrentSession()
		.get(Destination.class, destination.getId());
	dest.setName(destination.getName());
	return dest;
    }

    @Override
    public Destination find(Long id) {
	Query query = sessionFactory.getCurrentSession().createQuery(
		"from Destination d WHERE d.id = " + id);
	/*
	 * Query query = session.getNamedQuery("Destination.findById")
	 * .setParameter("id", id);
	 */
	Destination destination = (Destination) query.uniqueResult();
	return destination;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Destination> findAll() {
	List<Destination> destinations = sessionFactory.getCurrentSession()
		.getNamedQuery("Destination.findAll").list();
	return destinations;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Destination> findAllByCompany(Long companyId) {
	List<Destination> destinations = sessionFactory
		.getCurrentSession()
		.createQuery(
			"from Destination d where d.company.id = :companyId")
		.setParameter("companyId", companyId).list();
	return destinations;
    }

}
